package com.mrx.compose.test.ui.test

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import com.mrx.compose.test.model.UserModel
import com.mrx.compose.test.ui.BaseWindow

object MainWindow : BaseWindow() {

    override val title: String
        get() = "HelloWorld"

    override val size: DpSize
        get() = DpSize(240.dp, 320.dp)

    @Composable
    override fun app(window: ComposeWindow) {
        val dialogState = remember { mutableStateOf(false) }
        val dialogUserModel = remember { mutableStateOf(UserModel.DEFAULT_USER) }
        Dialog(
            visible = dialogState.value,
            title = "UserInfo",
            onCloseRequest = { dialogState.value = false }) {
            Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.maxWidthBroder()) {
                val model = dialogUserModel.value
                Image(model.image, "userImage")
                Text("Hello ${model.userName}, ${model.description}")
            }
        }
        (1..10).map { UserModel("admin $it", "i am admin $it") }.forEach {
            userCard(it) { model ->
                dialogState.value = true
                dialogUserModel.value = model
            }
        }
    }

    @Composable
    @OptIn(ExperimentalFoundationApi::class)
    private fun userCard(userModel: UserModel, onClick: (UserModel) -> Unit = {}) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.maxWidthBroder().padding(10.dp).onClick { onClick(userModel) },
        ) {
            Image(userModel.image, "logo", Modifier.size(32.dp))
            Column(modifier = Modifier.padding(start = 10.dp)) {
                Row(modifier = Modifier.maxWidthBroder()) {
                    Text(userModel.userName)
                }
                Row(modifier = Modifier.maxWidthBroder()) {
                    Text(userModel.description)
                }
            }
        }
    }

    @Composable
    fun testDialog(onClose: () -> Unit) {
        val dialogState = remember { mutableStateOf(true) }
        Dialog(
            visible = dialogState.value,
            title = "UserInfo",
            onCloseRequest = { dialogState.value = false;onClose() }) {
            testContent()
        }
    }

    @Composable
    private fun testContent() {
        val scrollState = rememberScrollState(0)
        Row {
            Column(Modifier.verticalScroll(scrollState).weight(1F)) {
                repeat(10) {
                    Column(
                        verticalArrangement = Arrangement.Center,
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.maxWidthBroder()
                    ) {
                        val model = UserModel.DEFAULT_USER
                        Image(model.image, "userImage")
                        Text("Hello ${model.userName}, ${model.description}")
                    }
                }
            }
            VerticalScrollbar(
                modifier = Modifier.border().fillMaxHeight(),
                adapter = rememberScrollbarAdapter(scrollState)
            )
        }
    }

    fun testApp() = application {
        Window(onCloseRequest = ::exitApplication) {
            app2()
        }
    }

    @Composable
    @Preview
    fun app2() {
        var text by remember { mutableStateOf("Hello, World!") }

        MaterialTheme {
            Button(onClick = {
                text = "Hello, Desktop!"
            }) {
                Text(text)
            }
        }
    }

}